package com.zzt.blog.controller;

import com.zzt.blog.pojo.dataobject.BlogInfo;
import com.zzt.blog.pojo.request.AddBlogRequest;
import com.zzt.blog.pojo.request.UpdateBlogRequest;
import com.zzt.blog.pojo.response.BlogInfoResponse;
import com.zzt.blog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-04-03
 * Time: 15:51
 */
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList(){
        log.info("获取博客列表...");
        List<BlogInfoResponse> blogInfos = blogService.getList();
        return blogInfos;
    }

    /**
     * 获取博客详情
     * @param blogId 博客ID
     * @return  博客详情
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull(message = "blogId不能为null") Integer blogId){
        log.info("getBlogDetail, blogId:{}", blogId);
        //TODO 参数校验
        return blogService.getBlogDetail(blogId);
    }

    /**
     * 添加博客
     * @param addBlogRequest 博客ID、标题、内容
     * @return true 成功  false 失败
     */
    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody @Validated AddBlogRequest addBlogRequest){
        log.info("发布博客， userId: {}, title: {}", addBlogRequest.getUserId(), addBlogRequest.getTitle());
        return blogService.addBlog(addBlogRequest);
    }

    /**
     * 编辑博客
     * @param updateBlogRequest 修改的内容
     * @return true 成功 false 失败
     */
    @RequestMapping("/update")
    public Boolean update(@RequestBody @Validated UpdateBlogRequest updateBlogRequest){
        log.info("更新博客，request:{}", updateBlogRequest);
        return blogService.updateBlog(updateBlogRequest);
    }

    @RequestMapping("/delete")
    public Boolean delete(@NotNull(message = "id不能为null") Integer blogId){
        log.info("删除博客, id:{}", blogId);
        return blogService.deleteBlog(blogId);
    }
}
